'use strict';

const Controller = require('egg').Controller;

class CategoryController extends Controller {
    // 新增类目：post
    async addCategory(){
        const {ctx} = this;
        const {category,icon}  = ctx.request.body
        ctx.validate(
            {
                category: {type:"nullValue", tips:'请填写类目'},
                icon: {type: "nullValue", tips:'请上传图标'}
            },
            ctx.request.body
        )
        // 查询是否重复
        const db = ctx.model.Category
        const repeat = await db.find({categoryName:category})

        if(repeat.length > 0){
            ctx.send([],422,'类目出现重复')
        }else{
            const res = await db.create({categoryName: category, icon})
            ctx.send(res)
        }
    }
    // 拿到类目：get
    async getCategory(){
        const {ctx} = this
        const {page} = ctx.request.body
        ctx.validate({
            page:{type: 'nullValue', tips:'分页值不能为空'}
        },
            ctx.request.body
    )
    const db = ctx.model.Category
    // const res = db.
    }
}

module.exports = CategoryController;